package com.test.demo.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.test.demo.R;
import com.test.demo.main.bean.MediumBean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class ViewPager2Activity extends AppCompatActivity {

    List<MediumBean> dataList = new ArrayList<>();
    private static final String URL_DATA = "NET_DATA";
    private static final String POSITION = "position";
    ViewPagerAdapter viewPagerAdapter;

    ViewPager2 viewPager;
    TextView tvTitle;
    private int currentItem;

    public static void start(Context context, List<MediumBean> urlList) {
        startNetUrl(context, urlList, 0);
    }

    public static void startNetUrl(Context context, List<MediumBean> urlList, int position) {
        Intent starter = new Intent(context, ViewPager2Activity.class);
        starter.putExtra(URL_DATA, (Serializable) urlList);
        starter.putExtra(POSITION, position);
        context.startActivity(starter);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        viewPager = findViewById(R.id.view_pager);
        tvTitle = findViewById(R.id.tv_title);
        resolveData();
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                playPosition(position, true);

            }
        });

        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
//        GSYVideoManager.onResume(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        GSYVideoManager.releaseAllVideos();
    }

    private void resolveData() {
        dataList.addAll((List<MediumBean>) getIntent().getSerializableExtra(URL_DATA));
        currentItem = getIntent().getIntExtra(POSITION, 0);
        tvTitle.setText((currentItem + 1) + "/" + dataList.size());
        viewPagerAdapter = new ViewPagerAdapter(this, dataList);
        viewPager.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setCurrentItem(currentItem);
        viewPagerAdapter.notifyDataSetChanged();

        viewPager.postDelayed(new Runnable() {
            @Override
            public void run() {
                playPosition(currentItem, false);
            }
        }, 100);
    }

    private int indexLoop = 0;

    private void playPosition(int position, boolean isNeedPause) {
        indexLoop = indexLoop + 1;
        RecyclerView.ViewHolder viewHolder = ((RecyclerView) viewPager.getChildAt(0)).findViewHolderForAdapterPosition(position);
        if (viewHolder instanceof RecyclerItemImageHolder) {
            RecyclerItemImageHolder imageHolder = (RecyclerItemImageHolder) viewHolder;
            imageHolder.onBind(0, dataList.get(position));

        }
        if (viewHolder == null && indexLoop < 3) {
            viewPager.setCurrentItem(position);
            viewPagerAdapter.notifyDataSetChanged();
            playPosition(position, false);
        }

        currentItem = position;
        tvTitle.setText((currentItem + 1) + "/" + dataList.size());
    }

}



